package wxk.interview.syn;

/**
 * @author wangxuekai
 * @version 2.2
 * @Date 2017/3/15.
 * @since 2.2
 */
public class CMB {
    private  int money;

    public synchronized void increase(int m) {
        System.out.println(Thread.currentThread().getName()+",开始增加金额:"+m);
        money = money + m;
        System.out.println(Thread.currentThread().getName()+",增加后为:"+money);
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public  void deduct(int m){
        synchronized (this) {
            System.out.println(Thread.currentThread().getName() + ",开始扣减金额:" + m);
            money = money - m;
            System.out.println(Thread.currentThread().getName() + ",扣减后为:" + money);
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "CMB{" +
                "money=" + money +
                '}';
    }
}
